package com.cm.figure.controller.upload;

import com.cm.figure.model.Data.Data;
import com.cm.figure.model.GetDate;
import com.cm.figure.model.Pictures;
import com.cm.figure.model.Server;
import com.cm.figure.model.User;
import com.cm.figure.service.uploadService.UploadService;
import com.cm.figure.utility.BuildData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * ClassName:uploadController
 * Package:com.cm.figure.controller.upload
 * Description:
 *
 * @Date:2021/8/15 19:51
 * @com.chuangmei
 */
@Controller
@RequestMapping("/api/private/figure")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    // 最大字节数
    private static final int biggestBytes = 2097152;

    @PostMapping("/upload")
    @ResponseBody
    public Data uploadImg(@RequestParam("file") MultipartFile multipartFile, User user) throws IOException {

        // data
        Data data = null;

        //后缀
        String[] suffixs = multipartFile.getOriginalFilename().split("\\.");
        String suffix = "." + suffixs[suffixs.length - 1];

        // 格式支持
        if (!".jpg".equals(suffix) && !".png".equals(suffix) && !".gif".equals(suffix) && !".jpeg".equals(suffix)) {
            return BuildData.buildData(null,"上传失败! 只支持 [jpg png gif] 格式!", 401);
        }

        // 最大2mb
        if (multipartFile.getSize() > this.biggestBytes) {
            return BuildData.buildData(null,"上传失败! 最大只支持2MB!", 401);
        }

        // 文件名
        String fileName = user.getId() + "." + GetDate.getCurrentDate("yyyyMMddHHmmssSSS") + suffix;

        //从user中获取上传路径
        String uploadPath = user.getFilepath();

        // 上传文件
        File uploadFile = new File(uploadPath);

        //如果目录不存在则创建
        if (!uploadFile.exists()){
            uploadFile.mkdirs();
        }

        // Pictures
        Pictures pictures = new Pictures();

        // 设置名称
        pictures.setFilename(fileName);
        pictures.setFilepath(uploadPath);
        pictures.setFilesize(multipartFile.getSize());
        pictures.setUserid(user.getId());
        pictures.setUploadtime(GetDate.getCurrentDate("yyyy-MM-dd HH:mm:ss"));

        // 插入图片
        int count = uploadService.addPicture(pictures);

        if (count > 0) {
            // System.out.println("插入数据库成功");
            // 上传文件
            File file = new File(uploadFile,fileName);

/*            System.out.println("file: " + file.toString());
            System.out.println("uploadFile: " + uploadFile);
            System.out.println("fileName: " + fileName);*/

            try {
                multipartFile.transferTo(file); // 上传
                if (Server.isLocal){
                    pictures.setUrl(Server.imagePathPrefixLocal + fileName);
                }else {
                    pictures.setUrl(Server.imagePathPrefixServer + fileName);
                    // System.out.println("url: " + Server.imagePathPrefixServer + pictures.getFilepath() + "/" + fileName);
                }
                data = BuildData.buildData(pictures,"上传成功!", 200);
            } catch (IOException e) { // 发生异常 , 上传失败
                e.printStackTrace();
                data = BuildData.buildData(pictures,"上传时发生异常,上传失败!", 400);
            }
        }else { // 插入数据失败 , 上传失败
            data = BuildData.buildData(pictures,"插入图片数据失败,上传失败!", 400);
        }

        return data;
    }

}
